package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.ArticleDTO;
import com.andy.blog.admin.dto.ArticleHomeDTO;
import com.andy.blog.admin.dto.ArticleHomeDetailDTO;
import com.andy.blog.admin.dto.ArticleRecommendDTO;
import com.andy.blog.admin.dto.ArticleTagInfoDTO;
import com.andy.blog.admin.service.ArticleBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.vo.ArticleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023/6/18 17:11
 */
@RestController
@Api(tags = "博客文章")
public class ArticleController {

    @Resource
    private ArticleBusService articleBusService;

    /**
     * 根据条件分页查询
     */
    @ApiOperation(value = "根据条件分页查询")
    @GetMapping("/article/selectListByPage")
    public GeneralResponseResult<PageUtilsDTO<ArticleTagInfoDTO>> selectListByPage(ArticleVO articleVO) {
        return GeneralResponseResult.success(articleBusService.selectListByPage(articleVO));
    }

    /**
     * 条件查询
     */
    @GetMapping("/article/selectList")
    public GeneralResponseResult<List<ArticleDTO>> selectList(ArticleVO articleVO) {
        return GeneralResponseResult.success(articleBusService.selectList(articleVO));
    }

    /**
     * 新增
     */
    @Log(title = "文章新增", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/article/addArticle")
    public GeneralResponseResult<Void> addArticle(@Validated @RequestBody ArticleVO articleVO) {
        articleBusService.addArticle(articleVO);
        return GeneralResponseResult.success();
    }

    /**
     * 查询详细
     */
    //@PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/article/{id}")
    public GeneralResponseResult<ArticleDTO> getById(@PathVariable Long id) {
        return GeneralResponseResult.success(articleBusService.selectArticleById(id));
    }

    /**
     * 修改
     */
    //@PreAuthorize("@ss.hasPermi('system:dept:edit')")
    @Log(title = "文章修改", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/article")
    public GeneralResponseResult<Void> updateArticle(@Validated @RequestBody ArticleVO articleVO) {
        articleBusService.updateArticle(articleVO);
        return GeneralResponseResult.success();
    }


    /**
     * 查看门户文章列表
     */
    //@VisitLogger(value = "首页")
    //@ApiOperation(value = "查看首页文章列表")
    @GetMapping("/portal/article/list")
    public GeneralResponseResult<PageUtilsDTO<ArticleHomeDTO>> listArticleHome(ArticleVO articleVO) {
        return GeneralResponseResult.success(articleBusService.listArticleHome(articleVO));
    }

    /**
     * 查看文章信息
     */
    @GetMapping("/portal/article/{articleId}")
    public GeneralResponseResult<ArticleHomeDetailDTO> getArticleHomeById(@PathVariable("articleId") Long articleId) {
        return GeneralResponseResult.success(articleBusService.getArticleHomeById(articleId));
    }

    /**
     * 查看推荐文章
     */
    @GetMapping("/portal/article/getRecommend")
    public GeneralResponseResult<List<ArticleRecommendDTO>> getRecommend() {
        return GeneralResponseResult.success(articleBusService.listArticleRecommendVO());
    }
}
